import { AppCTX } from "../../types/koaExtend";
import Router from "koa-router";
import {
  getBrandDistributeRequestByBrandIDAndUserID,
  applyBrandDistributeRequest,
} from "../../service/brandDistributeRequestService";
import {
  getBrandEndorseRequestByBrandIDAndUserID,
  applyBrandEndorseRequest,
} from "../../service/brandEndorseRequestService";
import {
  getStoreEndorseRequestByStoreIDAndUserID,
  applyStoreEndorseRequest,
} from "../../service/storeEndorseRequestService";
import {
  getBrandEndorsementByBrandIDSafely,
  getBrandDistributeEndorsementByBrandIDSafely,
} from "../../service/brandService";
import { getStoreEndorsementByStoreIDSafely } from "../../service/storeService";
import { listNormalBrandSpokesmanWithBrandsByUserID } from "../../service/brandSpokesmanService";
import { listNormalStoreSpokesmanWithStoresByUserID } from "../../service/storeSpokesmanService";
import { listActivityItemOfU2BWithStatAndBrandInfosByUserID } from "../../service/activityService";
import pageMW from "../../middleware/pager";
import { isString, isUndefined, _pick } from "mmb";
const router = new Router();

// 获取用户准备代言品牌的设置及已代言申请(OBSOLETE)
router.get("/usr/endorsement/setting", async (ctx: AppCTX, next) => {
  const { brandId } = ctx.query;
  const { userId } = ctx;
  if (!isString(brandId)) {
    ctx.throw(400, "brand is invalid");
  }

  const brandEndorsement = await getBrandEndorsementByBrandIDSafely(brandId);

  ctx.body = {
    brandEndorsement: {
      ..._pick(
        brandEndorsement,
        "isOpen",
        "spokesmanTitle",
        "isEntranceDisplay"
      ),
    },
  };
});

// 获取用户准备分销品牌的设置及已分销申请
router.get(
  "/usr/endorsement/settingAndDistributeRequest",
  async (ctx: AppCTX, next) => {
    const { brandId } = ctx.query;
    const { userId } = ctx;
    if (!isString(brandId)) {
      ctx.throw(400, "brand is invalid");
    }
    const brandDistributeEndorsement =
      await getBrandDistributeEndorsementByBrandIDSafely(brandId);
    const getBrandDistributeRequestByBrandIDAndUserIDRet =
      await getBrandDistributeRequestByBrandIDAndUserID(brandId, userId);

    ctx.body = {
      ..._pick(
        brandDistributeEndorsement,
        "isOpen",
        "distributorTitle",
        "needApplyInfo",
        "applyInfoFields",
        "isEntranceDisplay"
      ),
      auditResult: isUndefined(
        getBrandDistributeRequestByBrandIDAndUserIDRet.brandDistributeRequest
      )
        ? undefined
        : getBrandDistributeRequestByBrandIDAndUserIDRet.brandDistributeRequest
            .auditResult,
      distributorId:
        getBrandDistributeRequestByBrandIDAndUserIDRet.distribtorId,
    };
  }
);

// 用户申请品牌分销
router.post("/usr/endorsement/distributeRequest", async (ctx: AppCTX, next) => {
  const { brandId, applyFields } = ctx.request.body;
  const { userId } = ctx;
  if (!isString(brandId)) {
    ctx.throw(400, "brand is invalid");
  }
  const brandDistributeRequest = await applyBrandDistributeRequest({
    userId,
    brandId,
    applyFields,
  });
  ctx.body = {
    auditResult: isUndefined(brandDistributeRequest)
      ? undefined
      : brandDistributeRequest.auditResult,
  };
});

// 获取用户准备代言品牌的设置及已代言申请
router.get(
  "/usr/endorsement/settingAndEndorseBrandRequest",
  async (ctx: AppCTX, next) => {
    const { brandId } = ctx.query;
    const { userId } = ctx;
    if (!isString(brandId)) {
      ctx.throw(400, "brandId is invalid");
    }
    const brandEndorsement = await getBrandEndorsementByBrandIDSafely(brandId);
    const brandEndorseRequest = await getBrandEndorseRequestByBrandIDAndUserID(
      brandId,
      userId
    );

    ctx.body = {
      ..._pick(
        brandEndorsement,
        "isOpen",
        "spokesmanTitle",
        "needApplyInfo",
        "applyInfoFields",
        "isEntranceDisplay"
      ),
      auditResult: isUndefined(brandEndorseRequest)
        ? undefined
        : brandEndorseRequest.auditResult,
    };
  }
);

// 用户申请品牌代言
router.post(
  "/usr/endorsement/endorseBrandRequest",
  async (ctx: AppCTX, next) => {
    const { brandId, applyFields } = ctx.request.body;
    const { userId } = ctx;
    if (!isString(brandId)) {
      ctx.throw(400, "brandId is invalid");
    }
    const brandEndorseRequest = await applyBrandEndorseRequest({
      userId,
      brandId,
      applyFields,
    });
    ctx.body = {
      auditResult: isUndefined(brandEndorseRequest)
        ? undefined
        : brandEndorseRequest.auditResult,
    };
  }
);

// 获取用户准备代言门店的设置及已代言申请
router.get(
  "/usr/endorsement/settingAndEndorseStoreRequest",
  async (ctx: AppCTX, next) => {
    const { storeId } = ctx.query;
    const { userId } = ctx;
    if (!isString(storeId)) {
      ctx.throw(400, "storeId is invalid");
    }
    const storeEndorsement = await getStoreEndorsementByStoreIDSafely(storeId);
    const storeEndorseRequest = await getStoreEndorseRequestByStoreIDAndUserID(
      storeId,
      userId
    );

    ctx.body = {
      ..._pick(
        storeEndorsement,
        "isOpen",
        "spokesmanTitle",
        "needApplyInfo",
        "applyInfoFields",
        "isEntranceDisplay"
      ),
      auditResult: isUndefined(storeEndorseRequest)
        ? undefined
        : storeEndorseRequest.auditResult,
    };
  }
);

// 用户申请门店代言
router.post(
  "/usr/endorsement/endorseStoreRequest",
  async (ctx: AppCTX, next) => {
    const { storeId, applyFields } = ctx.request.body;
    const { userId } = ctx;
    if (!isString(storeId)) {
      ctx.throw(400, "storeId is invalid");
    }
    const storeEndorseRequest = await applyStoreEndorseRequest({
      userId,
      storeId,
      applyFields,
    });
    ctx.body = {
      auditResult: isUndefined(storeEndorseRequest)
        ? undefined
        : storeEndorseRequest.auditResult,
    };
  }
);

// 获取用户代言的品牌
router.get("/usr/endorsement/brands", pageMW, async (ctx: AppCTX, next) => {
  const { userId, limit, offset } = ctx;

  const { total, items } = await listNormalBrandSpokesmanWithBrandsByUserID(
    userId,
    {
      limit,
      offset,
    }
  );

  ctx.body = {
    total,
    rows: items.map((it) => ({
      ..._pick(it, "brandId", "brandName", "brandPic", "brandFansCount", "brandPublishActivityCount"),
    })),
  };
});

// 获取用户代言的店铺
router.get("/usr/endorsement/stores", pageMW, async (ctx: AppCTX, next) => {
  const { userId, limit, offset } = ctx;

  const { total, items } = await listNormalStoreSpokesmanWithStoresByUserID(
    userId,
    {
      limit,
      offset,
    }
  );

  ctx.body = {
    total,
    rows: items.map((it) => ({
      ..._pick(it, "storeId", "storeName", "storePic", "storeFansCount", "storePublishActivityCount"),
    })),
  };
});

// 获取代言品牌发布的活动
router.get("/usr/endorsement/activities", pageMW, async (ctx: AppCTX, next) => {
  const { userId, limit, offset, ts } = ctx;

  const { total, items } =
    await listActivityItemOfU2BWithStatAndBrandInfosByUserID(userId, {
      limit,
      offset,
      ts,
    });

  ctx.body = {
    total,
    rows: items,
  };
});

export default router;
